#include "idft.h"

#include <math.h>

static const long double PI = 4.0L * atan(1.0L);

// calculeaza transformata Fourier inversa avand ca intrare seria si ordinul termenului
Point idft(const FourierSerie &serie, int k, unsigned int n)
{
	double p, x, y;
	Point result;

	x = 0.0;
	y = 0.0;

	if (!n) n = serie.back().second + 1; // presupunem ca ultimul termen are rang 
										 // maxim si mai presupunem ca primul 
										 // termen are rang 0

	for(FourierSerie::const_iterator it_serie = serie.begin(); 
								it_serie != serie.end(); it_serie++)
	{
		const FourierTerm &current = *it_serie;
		const Complex &term = current.first;
		int u = current.second;

		p = -2.0L * PI * u * k / n;
		x +=  term.first * cos(p) + term.second * sin(p);
		y += -term.first * sin(p) + term.second * cos(p);
	}

	result.first  = (int)(x + 0.5);
	result.second = (int)(y + 0.5);

	return result;
}
